<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    
	
	<title>_创建预包装计划</title>
	<script type="text/javascript">
	  
	  //页面初始
      $(function() {
			var btn=$("#btn_createPlan");
		    btn.attr("disabled",false);
		    btn.attr("class","btn btn-primary");
		    $("#_customerCode").val("");
      });
	  
	
	//查询商品
	function skuQuery(){
	
	    var sku=$("#sku").val().trim();
	    var customerCode=$("#_customerCode").val();
	    var warehouseCode=$("#_warehouseOutCode").val();
	    
	    if(customerCode==''){
	       alert("请选择商家！");
	       $("#_customerCode").focus();
	       return;
	    }
	    
	    if(warehouseCode==''){
	       alert("请选择出货仓库！");
	       $("#_warehouseOutCode").focus();
	       return;
	    }
	    
	    $("#sku_content").html('');
	    
	    $.ajax({
			type : 'post',
			url : '${ctx}/outstorageBill/ajaxQuerySku',
			data : {
				customerCode:customerCode,
				warehouseCode:warehouseCode,
				sku:sku
			},
			dataType : 'json',
			success : function(data) {
				var stockList=data.stockList;
				var str="";
				$.each(stockList,function(i,stock){
				
					str+="<tr>";
					str+="<td>"+(i+1)+"</td>";
					str+="<td id='customerCode_"+stock.id+"'>"+stock.customer.customerCode+"</td>";
					str+="<td id='warehouseCode_"+stock.id+"'>"+stock.warehouseCode+"</td>";
					str+="<td id='sku_"+stock.id+"'>"+stock.sku+"</td>";
					str+="<td id='salesNum_"+stock.id+"'>"+stock.salesNum+"</td>";
					
					//判断是否已经添加
					var flag='1';
					$("[name='add']").each(function(i){
					    var tempId=this.id;
					    if(tempId==stock.id){
					        flag='2';
					    }
					});
					if(flag=='1'){
					   str+="<td>"+"<input type='button' class='btn btn-info'  value='添加' id='btn_"+stock.id+"' onclick='add("+stock.id+")'>"+"</td>";
					}else if(flag=='2'){
					   str+="<td>"+"<input type='button' class='btn'  value='已添加'  id='btn_"+stock.id+"'>"+"</td>";
					}
					
					str+="</tr>";
				});
				$("#sku_content").html(str);
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
		}); 
	}
	
	//添加商品
	function add(id){
	    var customerCode=$("#customerCode_"+id).html();
	    var warehouseCode=$("#warehouseCode_"+id).html();
	    var sku=$("#sku_"+id).html();
	    var salesNum=$("#salesNum_"+id).html();
	    
	    var trElement="";
	    trElement+="<tr name='add' id='"+id+"'>";
	    trElement+="<td id='add_customerCode_"+id+"'>"+customerCode+"</td>";
	    trElement+="<td id='add_warehouseCode_"+id+"'>"+warehouseCode+"</td>";
	    trElement+="<td id='add_sku_"+id+"'>"+sku+"</td>";
	    trElement+="<td>"+salesNum+"</td>";
	    trElement+="<td ><input type='text' id='add_num_"+id+"'/><span  id='add_message_"+id+"' style='color:red;'></span></td>";
	    trElement+="<td><input type='button' class='btn btn-info' value='取消' onclick='del("+id+");'/></td>";
	    trElement+="</tr>";
	    
	    $("#add_info").append(trElement);
	    
	    var btn=$("#btn_"+id);
	    btn.attr("disabled",true);
           btn.attr("class","btn");
           btn.val("已添加");
	    
	    $("#nodata").hide();
	}
	
	//取消商品
	function del(id){
	    $("#"+id).remove();
	    var btn=$("#btn_"+id);
	    btn.attr("disabled",false);
        btn.attr("class","btn  btn-info");
        btn.bind("click","add("+id+")");
        btn.val("添加");
	}
	
	//创建出库单
	function createPlan(){
	    
	    var btn=$("#btn_createPlan");
	    btn.attr("disabled",true);
	    btn.attr("class","btn");
	
	    var jsonArray=[];
	    var flag=true;
	    var flag_color=true;
	    
	    var customerCode=$("#_customerCode").val();
	    var warehouseCode_out=$("#_warehouseOutCode").val();
	    
	    //检查数据，并创建json对象
	    $("[name='add']").each(function(i){
	       var id=this.id;
	       var _customerCode=$("#add_customerCode_"+id).html();
	       var _warehouseCode=$("#add_warehouseCode_"+id).html();
	       var _sku=$("#add_sku_"+id).html();
	       var _num=$("#add_num_"+id).val().trim();
	       
	       if( isNaN(_num) || _num==''){
               $("#add_message_"+id).html("请输入整数!");
               flag=false;
           }else{
               $("#add_message_"+id).html("");
           }
           
           if(_customerCode!=customerCode || _warehouseCode!=warehouseCode_out ){
                $("#"+id).attr("bgcolor","yellow");
                flag_color=false;
           }else{
                $("#"+id).attr("bgcolor",""); 
           }
           
           var obj=createObj(_customerCode,_warehouseCode,_sku,_num);
           jsonArray.push(obj);
           
	    });
	    
	    if(jsonArray.length==0){
	       alert("请选择出库商品！");
	       btn.attr("disabled",false);
	       btn.attr("class","btn btn-primary");
	       return;
	    }
	    
	    if(flag==false || flag_color==false){
	      btn.attr("disabled",false);
	      btn.attr("class","btn btn-primary");
	      return;
	    }
	    
	    var jsonList= eval(jsonArray); 
	    var jsonText= JSON.stringify(jsonList);
	    
	    $.ajax({
			type:'post',
			url:'${ctx}/prepkgPlan/ajaxCreatePlan',
			data:{
			    customerCode:customerCode,
			    warehouseCode:warehouseCode_out,
				jsonText:jsonText
			},
			dataType:'json',
			success:function(data) {
			    var result=data.result;
			    if(result.length>0){ //标黄提示库存不满足
			       var skuArray=result.split(",");
			       for(var i=0; i<skuArray.length;i++){
			          var sku=skuArray[i];
			          $("[name='add']").each(function(i){
	                         var id=this.id;
	                         var _sku=$("#add_sku_"+id).html();
	                         if(sku==_sku){
	                            $("#"+id).attr("bgcolor","yellow");
	                            $("#add_message_"+id).html("库存不满足！");
	                         }
	                         
	                  });
			       }
			       
			    btn.attr("disabled",false);
	    		btn.attr("class","btn btn-primary");
			       
			    }else{
			       window.location.href = "${ctx}/prepkgPlan/toPrepkgPlanMgrPage";
			    }
			    
			   
			},
			error:function() {
		    	btn.attr("disabled",false);
	    		btn.attr("class","btn btn-primary");
				alert("网络繁忙，请稍后重试...");
			}
		});
          
	}
	
	//创建JSON对象
	function createObj(customerCode,warehouseCode,sku,num){
	    return {
	        customerCode:customerCode,
	        warehouseCode:warehouseCode,
	        sku:sku,
	        num:num
	    };
	}
	
</script>
</head>

<body>
	
	<div style="height: 10px;"></div>
	
	<form class="form-search"  method="post">
	<table id="contentTable" class="table  table-bordered table-condensed">
	    <thead>
			<th style="vertical-align: middle; color: white;" colspan="13" bgcolor="#8EB4CB">创建预包装计划</th>
		</thead>
		
		<tr><td colspan="6">
		    <label style="margin-left: 10px;">商家名称：</label>
			 <select  name="_customerCode"  id="_customerCode"  class="input-medium required" onchange="ajaxAreaGroup(this,'_warehouseOutCode');">
				<option value=""></option>
				<c:forEach items="${customerList}" var="customer">
					<option value="${customer.customerCode}"
					<c:if test="${customerCode eq customer.customerCode}">selected=selected</c:if>
					>${customer.customerName}</option>
				</c:forEach>
			</select>
			
			<label style="margin-left: 10px;">出货仓库：</label>
			   <select  name="_warehouseOutCode"  id="_warehouseOutCode"  class="input-medium" >
			     <option value=""></option>  
			     <c:forEach items="${warehouseList}" var="warehouse">
			       <option value="${warehouse.groupCode}" 
			       <c:if test="${warehouseOutCode eq warehouse.groupCode }">selected=selected</c:if>
			       >${warehouse.groupName}</option>
			     </c:forEach>
			</select>
		
			<button id="btn_createPlan" class="btn btn-primary"  style="margin-left: 10px;" onclick="createPlan(); return false;">创建计划</button>
			
	   </td></tr>
	   
		<thead><tr>
		    <th>商家</th>
		    <th>仓库</th>
		    <th>sku</th>
		    <th>可用库存</th>
		    <th>出库数量</th>
			<th>操作</th>
		</tr></thead>
		<c:if test="${empty billList or billList.size() eq 0}">
	       <tr id="nodata"><td colspan="6" height="50" style="vertical-align:middle; text-align: center;color: gray;font-size: 20px;">无已选商品</td></tr>
	    </c:if>
		<tbody id="add_info"></tbody>
    </table>
    </form>
    
    
    
	<!-- 商品查询 -->
	<form class="form-search">
	<table id="contentTable" class="table table-striped table-bordered table-condensed">
	    <thead>
			<th style="vertical-align: middle; color: white; " colspan="13" bgcolor="#8EB4CB">商品查询</th>
		</thead>
		
		<tr><td colspan="6">
		    <label>SKU 编码：</label> 
			<input type="text"  id="sku"  class="input-medium" />
		    <button type="button" class="btn btn-primary" style="margin-left: 10px;" onclick="skuQuery();">搜索</button>
        </td></tr>
		
		<thead><tr>
		    <th>序号</th>
		    <th>商家</th>
		    <th>仓库</th>
		    <th>sku</th>
		    <th>可用库存</th>
			<th>操作</th>
		</tr></thead>
		<tbody id="sku_content">
		</tbody>
	</table>
	</form>
	
</body>
</html>
